﻿/* ----------------------------------------------------------------------------
 * Copyright (c) Philip Stears 
 *
 * This source code is subject to terms and conditions of the Microsoft Public 
 * License (Ms-PL).
 * 
 * By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 * 
 * To request a copy of the Microsoft Public License, send a request by email
 * to license@connectbasic.net.
 *
 * This notice must not be removed from this software.
 * ---------------------------------------------------------------------------- */

namespace ConnectBasic.Shell.Hosting
{
    using System;
    using ConnectBasic.Shell.Extensibility;
    using ConnectBasic.Shell.Implementation;

    /// <summary>
    /// Provides the default implementation of the shell host
    /// interface.
    /// </summary>
    internal class DefaultShellHost : IShellHost
    {
        private IShell mShell;
        private DefaultShellHostProjectCollection mProjects;

        #region .ctor

        /// <summary>
        /// Initializes a new instance of the <see cref="DefaultShellHost" />
        /// class using the <see cref="DefaultShell" /> implementation of the
        /// <see cref="IShell" /> interface.
        /// </summary>
        /// <param name="settings">The settings which describe how to setup the host.</param>
        internal DefaultShellHost(ShellHostSettings settings)
            : this(settings, null)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DefaultShellHost" />
        /// class using a factory method to create an appropriate implementation
        /// of a shell.
        /// </summary>
        /// <param name="settings">The settings which describe how to setup the host.</param>
        /// <param name="shellFactory">A factory method which will be used to create the shell for the host, may be a null reference to use the default factory.</param>
        internal DefaultShellHost(ShellHostSettings settings, Func<IShellHost, IShell> shellFactory)
        {
            mProjects = new DefaultShellHostProjectCollection(this);

            // Use the factory method to create an instance
            // of a shell bound to this host
            if (shellFactory == null)
            {
                mShell = new DefaultShell(this);
            }
            else
            {
                mShell = shellFactory(this);
            }            

            // Make sure we actually got a shell otherwise
            // there's no point in continuing.
            if (mShell == null)
            {
                throw new ShellCreationException();
            }
        }

        #endregion

        #region IShellHost Members

        IShell IShellHost.Shell
        {
            get { return mShell; }
        }

        IShellHostGlobals IShellHost.Globals
        {
            get { throw new NotImplementedException(); }
        }

        IShellHostProjectCollection IShellHost.Projects
        {
            get { return mProjects; }
        }

        #endregion

        #region IServiceProvider Members

        object IServiceProvider.GetService(Type serviceType)
        {
            return null;
        }

        #endregion

    }
}
